package com.example.cms.controller;

import com.example.cms.entity.Order;
import com.example.cms.pojo.Mesagge;
import com.example.cms.pojo.QueryPage;
import com.example.cms.service.OrderService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderController
 * @Descriptiom TODO
 * @Author Sunbird
 * @Date 2018/12/13 18:37
 * @Version 1.0
 **/
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(value ="/orders",method = RequestMethod.POST)
    public Mesagge getorders(Mesagge mesagge, QueryPage queryPage) throws ParseException {
        List<Order> list =orderService.findOrdePage(mesagge,queryPage);
        queryPage.setCurr_page((queryPage.getCurr_page()-1)*queryPage.getPageSize());
        SimpleDateFormat sdf  =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simdate1=new SimpleDateFormat("yyyy-MM-dd");
        for (int i =0; i<list.size();i++) {
//            Date time = list.get(i).getOrderDate();
//            String s= sdf.format(time).substring(0,10);
//            System.out.println(s);
//          Date time1=  simdate1.parse(s);
//          list.get(i).setOrderDate(time1);
           }
        mesagge.setList(list);
        return mesagge;
    }

    @RequestMapping(value = "/addOrder",method = RequestMethod.POST)
    public Mesagge addOrder(Order order ,Mesagge mesagge){
       int i= orderService.insertOrder(order);
        mesagge.setMesCode(i);
        System.out.println(order.toString());
       return mesagge;
    }

    @RequestMapping(value = "/updateOrder",method = RequestMethod.POST)
    public Mesagge updateOrder(Order order ,Mesagge mesagge){
        System.out.println(order.toString());
        int i =orderService.updateOrder(order);
        mesagge.setMesCode(i);
        return  mesagge;
    }

    @RequestMapping(value = "/deletOrder" ,method = RequestMethod.GET)
    public Mesagge deletOrder(Integer orderId,Mesagge mesagge){
        int i =orderService.deletOrder(orderId);
         mesagge.setMesCode(i);
        return mesagge;
    }
}
